<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">


var int = self.setInterval("checkNotifications()", 10000);

function checkNotifications() {
	check();
}

function check(){
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "viewFriends?do=check", true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var friend = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("friend")[0].firstChild.data;
				if (friend == "y")
					alert("Your friend added post");
				var author = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("author")[0].firstChild.data;
				if (author == "y")
					alert("Post added to your thread");
			} else
				alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
		}
	};
	xmlhttp.send(null);
}

function showFriend(){
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "editFriend?do=ajaxShowFriend&name="+document.getElementById('friendName').value, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				//process the response
				var mdiv = document.getElementById("friend");
				mdiv.innerHTML = xmlhttp.responseText;
				if (xmlhttp.responseText != "User with this username does not exist!!!"){
					document.getElementById("friendName").disabled = true;
					document.getElementById("buttonCheck").value="Add Friend";
					document.getElementById("buttonCheck").onclick=new Function("addFriend()");
				}
			} else
				alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
		}
	};
	xmlhttp.send(null);
}

function addFriend(){
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.open("POST", "editFriend?name="+document.getElementById('friendName').value, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				alert(xmlhttp.responseText);
				window.location = "viewFriends";
			} else
				alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
		}
	};
	xmlhttp.send(null);
}

</script>
<base href="<%=basePath%>">

<title>Add Friend</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>
<body background="background.jpg" bgcolor="AliceBlue" link="Navy" text="Navy" >
<font face="COMIC SANS MS" >
	<h1 align="center">
		<font size="10"  color="FloralWhite">Add friend</font>
	</h1>
	<form name="add" action="editFriend" method="post">
		<table border="1" align="center" width=30% height=150 >
			<tbody>
				<tr>
					<td align="center"><font size="5" >Friend Name:</font></td>
					<td><font size="5" ><input type="text" id="friendName" name="name" value="">
					</font></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><button id="buttonCheck" type="button" onclick='showFriend()'>Search Friend</button>
					<div id="friend"></div>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
	<br>
	<p align="center">
		<a size=5 href="logout">logout</a> <br>
		<a size=5 href="viewFriends">Back to Friends</a>
	</p>
</font>
</body>
</html>